package com.google.android.daca;

import java.net.URI;
import java.net.URISyntaxException;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MenuActivity extends Activity {

	private static final String CLASS_TAG = MenuActivity.class.getSimpleName();
	private LinearLayout containerView = null;
	private Button goToCreatePoll;
	private Button goToLoginPoll;
	private Button goToPollResults;
	private Button goToClosePoll;
	private Button goToDeletePoll;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.v(CLASS_TAG, "MenuActivity->onCreate()");
		setContentView(R.layout.menu);

		LinearLayout.LayoutParams linLayoutParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		String doodlePollID = null;
        Uri dataUri = getIntent().getData();
        if (dataUri != null)
        {
            try 
            {
                URI javaUri = new URI(dataUri.toString());
                doodlePollID = javaUri.getPath().replaceAll("/", "");
            }
            catch (URISyntaxException e)
            {
                Log.e(CLASS_TAG, "Invalid Doodle URI");
            }
        }
        final String finalDoodlePollID = doodlePollID;
        
		goToCreatePoll = new Button(this);
		goToCreatePoll.setText("Create Poll");
		goToCreatePoll.setWidth(200);
		goToCreatePoll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goToCreatePoll();
			}
		});
		
		goToLoginPoll = new Button(this);
		goToLoginPoll.setText("Login To Poll");
		goToLoginPoll.setWidth(200);
		goToLoginPoll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goToLoginPoll(finalDoodlePollID);
			}
		});
		
		goToPollResults = new Button(this);
		goToPollResults.setText("Go To Poll Results");
		goToPollResults.setWidth(200);
		goToPollResults.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goToPollResults(finalDoodlePollID);
			}
		});
		
		goToClosePoll = new Button(this);
		goToClosePoll.setText("Close Poll");
		goToClosePoll.setWidth(200);
		goToClosePoll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goToClosePoll(finalDoodlePollID);
			}
		});
		
		goToDeletePoll = new Button(this);
		goToDeletePoll.setText("Delete Poll");
		goToDeletePoll.setWidth(200);
		goToDeletePoll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				goToDeletePoll(finalDoodlePollID);
			}
		});
		
		linLayoutParams.gravity = Gravity.CENTER;
		containerView = (LinearLayout) findViewById(R.id.menu);
		linLayoutParams.setMargins(5, 5, 5, 5);
		
		//Dynamically add buttons to the screen
		containerView.addView(goToLoginPoll, linLayoutParams);
		containerView.addView(goToCreatePoll, linLayoutParams);
		containerView.addView(goToPollResults, linLayoutParams);
		containerView.addView(goToClosePoll, linLayoutParams);
		containerView.addView(goToDeletePoll, linLayoutParams);
	}

	/**
	 * Opens Create Poll activity
	 */
	private void goToCreatePoll() {
		Intent intent = new Intent(MenuActivity.this, CreatePollActivity.class);
		startActivity(intent);
	}
	
	/**
	 * Opens Login to Poll activity
	 */
	private void goToLoginPoll(String doodlePollID) {
		Intent loginPollIntent = new Intent(MenuActivity.this, LoginActivity.class);
		loginPollIntent.putExtra(getResources().getString(R.string.extra_poll_id), doodlePollID);
		startActivity(loginPollIntent);
	}
	
	/**
	 * Opens Close Poll activity
	 */
	private void goToClosePoll(String doodlePollID) {
		Intent closePollIntent = new Intent(MenuActivity.this, ClosePollActivity.class);
		closePollIntent.putExtra(getResources().getString(R.string.extra_poll_id), doodlePollID);
		startActivity(closePollIntent);
	}
	
	/**
	 * Opens Delete Poll activity
	 */
	private void goToDeletePoll(String doodlePollID) {
		Intent deletePollIntent = new Intent(MenuActivity.this, DeletePollActivity.class);
		deletePollIntent.putExtra(getResources().getString(R.string.extra_poll_id), doodlePollID);
		startActivity(deletePollIntent);
	}
	
	/**
	 * View Poll Results activity
	 */
	private void goToPollResults(String doodlePollID) {
		Intent pollResultsIntent = new Intent(MenuActivity.this, PollResultsIDSelectionActivity.class);
		pollResultsIntent.putExtra(getResources().getString(R.string.extra_poll_id), doodlePollID);
		startActivity(pollResultsIntent);
	}
}
